﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DosTexPrj.Core.Models;
using DosTexPrj.Core.RepositoriesInterface;
using DosTexPrj.Implement.NHibernate;
using DosTexPrj.UI.WinForm.Commons;

namespace DosTexPrj.UI.WinForm
{
    public partial class CustomerOrderDetailForm : Form
    {
        #region --- Private members ---

        private IOrderRepository _orderRepository;
        private IRepository _repository;

        #endregion --- Private members ---

        #region --- Properties ---

        public OrderDetail CurrentOrderDetail
        {
            get;
            private set;
        }

        #endregion --- Properties ---

        #region --- Constructors ---

        public CustomerOrderDetailForm()
        {
            InitializeComponent();
            _orderRepository = MyServiceLocator.DoGetInstance<IOrderRepository>();
            _repository = MyServiceLocator.DoGetInstance<IRepository>();
            this.CurrentOrderDetail = null;
            this.txtCuttableWidth.KeyPress += new KeyPressEventHandler(Textbox_KeyPress);
            this.txtPrice.KeyPress += new KeyPressEventHandler(Textbox_KeyPress);
            this.txtQttKg.KeyPress += new KeyPressEventHandler(Textbox_KeyPress);
            this.txtQttMt.KeyPress += new KeyPressEventHandler(Textbox_KeyPress);
            this.txtWeight.KeyPress += new KeyPressEventHandler(Textbox_KeyPress);
            this.txtCuttableWidth.MaxLength = this.txtPrice.MaxLength = this.txtQttKg.MaxLength
                = this.txtQttMt.MaxLength = this.txtWeight.MaxLength = 12;
        }

        private CustomerOrderDetailForm(OrderDetail orderDetailToEdit)
            : this()
        {
            this.CurrentOrderDetail = orderDetailToEdit;
        }

        #endregion --- Constructors ---

        private void CustomerOrderDetailForm_Load(object sender, EventArgs e)
        {
            // Clear form and load comboboxes
            ClearForm();
        }

        private void Textbox_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.HandlingNumberTextbox(e);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValidate())
            {
                this.CurrentOrderDetail = null;
                return;
            }
            this.CurrentOrderDetail = new OrderDetail();
            this.CurrentOrderDetail.FabricType = (FabricType)cboFabricType.SelectedItem;
            this.CurrentOrderDetail.Color = txtColor.Text.Trim();
            this.CurrentOrderDetail.Composition = (Composition)cboComposition.SelectedItem;

            double qttKg = 0;
            double.TryParse(txtQttKg.Text.Trim(), out qttKg);
            this.CurrentOrderDetail.KgQuantity = qttKg;

            double qttMt = 0;
            double.TryParse(txtQttMt.Text.Trim(), out qttMt);
            this.CurrentOrderDetail.MtQuantity = qttMt;

            int cuttableWidth = 0;
            int.TryParse(txtCuttableWidth.Text.Trim(), out cuttableWidth);
            this.CurrentOrderDetail.CuttableWidth = cuttableWidth;

            int weight = 0;
            int.TryParse(txtWeight.Text.Trim(), out weight);
            this.CurrentOrderDetail.Weight = weight;

            double price = 0;
            double.TryParse(txtPrice.Text.Trim(), out price);
            this.CurrentOrderDetail.Price = price;

            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        #region Functions

        private void LoadFabricType()
        {
            try
            {
                var fabricData = _repository.GetAll<FabricType>();
                IList<FabricType> fabrics = new List<FabricType>();

                fabrics = (from f in fabricData
                           select new FabricType
                               {
                                   Id = f.Id,
                                   FabricTypeName = f.FabricTypeName
                               }).ToList();

                if (fabrics.Count > 0)
                {
                    cboFabricType.DataSource = fabrics;
                    cboFabricType.DisplayMember = "FabricTypeName";
                    cboFabricType.ValueMember = "Id";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void LoadComposition()
        {
            try
            {
                var compositionData = _repository.GetAll<Composition>();
                IList<Composition> compositions = new List<Composition>();

                compositions = (from c in compositionData
                                select new Composition
                                {
                                    Id = c.Id,
                                    CompostionName = c.CompostionName
                                }).ToList();

                if (compositions.Count > 0)
                {
                    cboComposition.DataSource = compositions;
                    cboComposition.DisplayMember = "CompostionName";
                    cboComposition.ValueMember = "Id";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void ClearForm()
        {
            // Clear textboxes
            txtColor.Text = string.Empty;
            txtPrice.Text = string.Empty;
            txtQttKg.Text = string.Empty;
            txtQttMt.Text = string.Empty;
            txtCuttableWidth.Text = string.Empty;
            txtWeight.Text = string.Empty;
            // Load comboboxes
            LoadFabricType();
            LoadComposition();
        }

        private bool CheckValidate()
        {
            error.Clear();
            bool flag = true;
            if (!Common.IsBlankTextBox(txtColor, "Required field!", error))
                flag = false;
            if (!Common.IsBlankTextBox(txtPrice, "Required field!", error))
                flag = false;
            if (!Common.IsBlankTextBox(txtQttKg, "Required field!", error))
                flag = false;
            if (!Common.IsBlankTextBox(txtQttMt, "Required field!", error))
                flag = false;
            if (!Common.IsBlankTextBox(txtCuttableWidth, "Required field!", error))
                flag = false;
            if (!Common.IsBlankTextBox(txtWeight, "Required field!", error))
                flag = false;
            return flag;
        }

        #endregion Functions
    }
}